package com.scau.ums.service.imp.password;

import com.scau.ums.mapper.AdminMapper;
import com.scau.ums.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * author: 莫伟杰
 * date： 2019/7/26 14:43
 */
@Service
public class AdminPasswordImp extends PasswordImp {

    private AdminMapper adminMapper;

    @Autowired
    public AdminPasswordImp(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }

    //管理员重置密码为root
    @Override
    public boolean resetPassword(String userID) {
        Admin admin = adminMapper.selectByPrimaryKey(userID);
        if(admin==null) return false;
        admin.setPassword(encode("root"));
        return adminMapper.updateByPrimaryKey(admin)==1;
    }

    @Override
    public boolean changePassword(String userID, String oldPwd, String newPwd) {
        Admin admin = adminMapper.selectByPrimaryKey(userID);
        if(admin==null) return false;
        if(!matches(oldPwd,admin.getPassword())) return false;
        admin.setPassword(encode(newPwd));
        return adminMapper.updateByPrimaryKey(admin)==1;
    }
}
